package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.AssortProducts;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 分类商品请求监听类
 */
public abstract class GetAssortProductslListener extends BaseListener {

	private static final String TAG = GetAssortProductslListener.class
			.getSimpleName();

	@Override
	public void onOffline() {
	}
	public GetAssortProductslListener(RequestCode requestCode) {
		super(requestCode);
	}
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case 0:
				JSONObject result = response.getJSONObject("result");
				JSONArray listArray = result.getJSONArray("products_list");
				System.out.println(listArray.toString()+"+++++++++++++++++++++++++");
				List<AssortProducts> lists = new ArrayList<AssortProducts>();
				for (int i = 0; i < listArray.length(); i++) {
					JSONObject provinceJson = listArray.getJSONObject(i);
					AssortProducts info = new AssortProducts();
					//info.setId(provinceJson.getInt("id"));
					info.setBrandId(provinceJson.getInt("brand_id"));
					info.setShopId(provinceJson.getInt("shop_id"));
					info.setShopName(provinceJson.getString("shop_name"));
					info.setProductId(provinceJson.getInt("product_id"));
					info.setProductIntro(provinceJson.getString("product_title"));
					info.setProductIcon(provinceJson.getString("pic"));
					info.setOriginalCost(provinceJson.getDouble("tag_price"));
					info.setCurrentCost(provinceJson.getDouble("sale_price"));
					info.setSaleNum(provinceJson.getInt("sale_num"));
					info.setInventoryNum(provinceJson.getInt("num"));
					//info.setBrandName(provinceJson.getString("brand_name"));
					info.setOpenTime(provinceJson.getString("open_time"));
					lists.add(info);
				}
				onResponseSuccess(lists);
				break;
			case 2:
				onOffline();
				break;
			case 4:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
				break;
			}

		} catch (JSONException e) {
			onFailure(statusCode, headers, e, new JSONObject());
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}

	/**
	 * 商品一级列表数据请求成功
	 */
	public abstract void onResponseSuccess(
			List<AssortProducts> products);

}
